using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using MobileClient.UIProcessService;

namespace MobileClient
{
	/// <summary>
	/// Summary description for frmEntries.
	/// </summary>
	public class frmEntries : System.Windows.Forms.Form
	{
		private frmEntry dlgEntry;
		private int m_nIndex;
		private System.Windows.Forms.ListView lvwEntries;
		private System.Windows.Forms.ColumnHeader hdrItem;
		private System.Windows.Forms.ContextMenu mnuContext;
		private System.Windows.Forms.MenuItem mnuView;
		private MobileClient.ImageButton btnSave;
		private MobileClient.ImageButton btnCancel;
		private System.Windows.Forms.ImageList imgStatus;
        private MobileClient.UIProcessService.Form _theForm;
	
		public frmEntries()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			// create the image buttons
			this.btnSave = new MobileClient.ImageButton();
			this.btnCancel = new MobileClient.ImageButton();
			// 
			// btnSave
			// 
			this.btnSave.Location = new System.Drawing.Point(23, 245);
			this.btnSave.Size = new Size(72, 20);
			this.btnSave.ImageButtonUp = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MobileClient.Graphics.save_up.gif"));
			this.btnSave.ImageButtonDown = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MobileClient.Graphics.save_down.gif"));
			this.btnSave.Visible = true;
            this.btnSave.Text = MobileClient.Properties.Resources.lblSave;
			this.btnSave.Type = MobileClient.ImageButton.ButtonType.Button;
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.Location = new System.Drawing.Point(139, 245);
			this.btnCancel.Size = new Size(72, 20);
			this.btnCancel.ImageButtonUp = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MobileClient.Graphics.cancel_up.gif"));
			this.btnCancel.ImageButtonDown = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MobileClient.Graphics.cancel_down.gif"));
			this.btnCancel.Visible = true;
			this.btnCancel.Text = MobileClient.Properties.Resources.lblCancel;
			this.btnCancel.Type = MobileClient.ImageButton.ButtonType.Button;
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// add the buttons to the form
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnSave);
		}

        public MobileClient.UIProcessService.Form theForm
		{
			get { return _theForm; }
			set { _theForm = value; }
		}
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmEntries));
            this.lvwEntries = new System.Windows.Forms.ListView();
            this.hdrItem = new System.Windows.Forms.ColumnHeader();
            this.mnuContext = new System.Windows.Forms.ContextMenu();
            this.mnuView = new System.Windows.Forms.MenuItem();
            this.imgStatus = new System.Windows.Forms.ImageList();
            this.SuspendLayout();
            // 
            // lvwEntries
            // 
            this.lvwEntries.Columns.Add(this.hdrItem);
            this.lvwEntries.ContextMenu = this.mnuContext;
            this.lvwEntries.FullRowSelect = true;
            this.lvwEntries.LargeImageList = this.imgStatus;
            this.lvwEntries.Location = new System.Drawing.Point(4, 4);
            this.lvwEntries.Name = "lvwEntries";
            this.lvwEntries.Size = new System.Drawing.Size(232, 228);
            this.lvwEntries.SmallImageList = this.imgStatus;
            this.lvwEntries.TabIndex = 0;
            this.lvwEntries.View = System.Windows.Forms.View.Details;
            // 
            // hdrItem
            // 
            this.hdrItem.Text = MobileClient.Properties.Resources.lblFormEntries;
            this.hdrItem.Width = 215;
            // 
            // mnuContext
            // 
            this.mnuContext.MenuItems.Add(this.mnuView);
            // 
            // mnuView
            // 
            this.mnuView.Text = MobileClient.Properties.Resources.lblView;
            this.mnuView.Click += new System.EventHandler(this.mnuView_Click);
            this.imgStatus.Images.Clear();
            this.imgStatus.Images.Add(((System.Drawing.Image)(resources.GetObject("resource"))));
            this.imgStatus.Images.Add(((System.Drawing.Image)(resources.GetObject("resource1"))));
            // 
            // frmEntries
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
            this.ClientSize = new System.Drawing.Size(238, 295);
            this.Controls.Add(this.lvwEntries);
            this.Name = "frmEntries";
            this.Text = MobileClient.Properties.Resources.lblEntriesComplete;
            this.Load += new System.EventHandler(this.frmEntries_Load);
            this.ResumeLayout(false);

		}
		#endregion

        private void DisplayEntry(MobileClient.UIProcessService.FormResource objResource)
		{
			FormResourceDirection objDirection = new FormResourceDirection();

			while(objDirection.Direction != FormResourceDirection.FormResourceDirectionType.Home)
			{
				if(dlgEntry == null)
				{
					dlgEntry = new frmEntry();
				}

                if ((!(objResource is DataField)) && (!(objResource is Conditional)))
                {
                    dlgEntry.ResourceInfo = objResource;

                    if (dlgEntry.ShowDialog() == DialogResult.OK)
                    {
                        objResource = dlgEntry.ResourceInfo;
                        objResource.Completed = true;
                    }
                    objDirection = dlgEntry.Direction;
                }
                else
                {
                    objDirection = new FormResourceDirection();
                    objDirection.Direction = FormResourceDirection.FormResourceDirectionType.Next;
                }

				// figure out the direction to move in the
				// entry array
				switch(objDirection.Direction)
				{
					case FormResourceDirection.FormResourceDirectionType.Previous:
						m_nIndex--;
						if(m_nIndex < 0)
						{
							objDirection.Direction = FormResourceDirection.FormResourceDirectionType.Home;
						}
						break;
					case FormResourceDirection.FormResourceDirectionType.Next:
						m_nIndex++;
						if(m_nIndex >= _theForm.FormResources.Length)
						{
							objDirection.Direction = FormResourceDirection.FormResourceDirectionType.Home;
						}
						break;
				}

				if(objDirection.Direction != FormResourceDirection.FormResourceDirectionType.Home)
				{
					objResource = (FormResource)_theForm.FormResources[m_nIndex];
				}
			}

			LoadEntries();
		}

		private void frmEntries_Load(object sender, System.EventArgs e)
		{
			clsModifyButtons.HideDoneButton(clsModifyButtons.GetHWnd(this));
			clsModifyButtons.HideXButton(clsModifyButtons.GetHWnd(this));

			m_nIndex = 0;
			this.Text = _theForm.Name;
			LoadEntries();
			DisplayEntry((FormResource)_theForm.FormResources[0]);
		}

		private void LoadEntries()
		{
			string strTemp;
			ListViewItem lvItem;

			Cursor.Current = Cursors.WaitCursor;
			lvwEntries.Items.Clear();
			lvwEntries.BeginUpdate();

			foreach(FormResource objEntry in _theForm.FormResources)
			{
				strTemp = objEntry.Title;
				lvItem = new ListViewItem(strTemp);
				if(objEntry.Completed)
				{
					lvItem.ImageIndex = 0;
				}
				else
				{
					lvItem.ImageIndex = 1;
				}
				lvwEntries.Items.Add(lvItem);
			}
			
			lvwEntries.EndUpdate();
			Cursor.Current = Cursors.Default;
		}

		private void mnuView_Click(object sender, System.EventArgs e)
		{
			string strSelected = String.Empty;
			foreach(ListViewItem lvItem in lvwEntries.Items)
			{
				if(lvItem.Selected)
				{
					strSelected = lvItem.Text;
					break;
				}
			}

			int nPos = strSelected.IndexOf(" ");
			strSelected = strSelected.Substring(0, nPos);
			if(!String.IsNullOrEmpty(strSelected))
			{
                foreach (FormResource objFormEntry in _theForm.FormResources)
                {
                    if(objFormEntry.Title == strSelected)
                    {
                        DisplayEntry(objFormEntry);
                        break;
                    }
                }
			}
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			this.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
		}
	}
}
